
package com.compuvest.tld;

// <editor-fold defaultstate="collapsed" desc="import">
import javax.servlet.http.HttpServletResponse;
    
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import java.net.URLEncoder;

import com.compuvest.web.Preference;
// </editor-fold>
    
public class Preferencetld extends TagSupport {

    protected ArrayList<Preference> prefList;
    protected Map<Integer,String> popData;
    protected String pageUrl = null;
    protected String popUrl = null;
    protected StringBuilder body = null;
    protected StringBuilder list = null;

// <editor-fold defaultstate="collapsed" desc="doStartTag()">

    @Override
    public int doStartTag() throws JspException {

	if(popData == null) return SKIP_BODY;

	list = new StringBuilder(15000);
	try {
            for(int position = 0; position < prefList.size(); position++) {

		List<String> contents = prefList.get(position).getContents();
		if(contents == null) continue;

                list.append("<a class='tlt' onclick=\"fcl('a").append(position).append("');\"><img src='../img/it/pls.png' alt=''/><i>");
		list.append(prefList.get(position).getTitle()).append("</i></a>\n");
		list.append("<div id='a").append(position).append("' style='display:none;'><div class='al'>\n");

		// reset
		if(popData.get(position) != null) {
                    list.append("<a href='").append(pageUrl).append("&p").append(position).append(popUrl).append("' style='color:red;'>Reset</a>");
                }

		for(int selct = 0; selct < contents.size(); selct++) {
                    list.append("<a href='").append(pageUrl).append("&p").append(position).append("=");
                    list.append(URLEncoder.encode(contents.get(selct),"UTF-8")).append(popUrl).append("'>");
                    list.append(contents.get(selct)).append("</a>");
		}
		list.append("</div></div>\n");
            }
	}
	catch(java.io.UnsupportedEncodingException ignore) { }

	// if empty context -> exit
	if(list.length() == 0) return SKIP_BODY;

	body = new StringBuilder(20000);
	body.append("<div style='width:156px;margin-top:10px;'>");
	body.append("<fieldset><div style='background-color:#fcc43f;'>");
	body.append("<img src='../img/it/mns.png' style='display:none;' onLoad='prestart();' alt=''/>");
	body.append("<div class='adsear'>");
	body.append("<b>Search By Preference</b> | <a class='hlp' href='");
	body.append(pageContext.getServletContext().getInitParameter("http")).append("SearchTips.jsp'>Help</a></div>\n");
	body.append("<div id='pref'>\n");
	body.append(list);
	body.append("</div></div></fieldset></div>\n");

        try {
            pageContext.getOut().print(body.toString());
    	}
    	catch(java.io.IOException e) { throw new JspTagException("Preference: " + e.getMessage()); }

        return SKIP_BODY;
    }
// </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="doEndTag()">

    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="release()">

    @Override
    public void release() {
	pageUrl = null;
	prefList = null;
	popUrl = null;
	popData = null;
	body = null;
	list = null;
    }
// </editor-fold>

//----------------------------

    public void setPageUrl(String pageUrl)                          { this.pageUrl = pageUrl; }
    public void setPreferenceList(ArrayList<Preference> prefList)   { this.prefList = prefList; }
    public void setPopUrl(String popUrl)                            { this.popUrl = popUrl; }
    public void setPopData(Map<Integer,String> popData)             { this.popData = popData; }
}
